package com.ly.heart.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ly.common.core.domain.AjaxResult;
import com.ly.common.exception.CustomException;
import com.ly.common.utils.DateUtils;
import com.ly.common.utils.SecurityUtils;
import com.ly.common.utils.StringUtils;
import com.ly.common.utils.erp.ConstUtil;
import com.ly.common.utils.erp.SnowflakeIdWorker;
import com.ly.heart.domain.KingReimburseLevelEntity;
import com.ly.heart.domain.dto.StartProcessDto;
import com.ly.heart.mapper.KingReimburseLevelMapper;
import com.ly.heart.service.IActModelService;
import com.ly.heart.service.IKingReimburseLevelService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ly.heart.util.CurrentUserUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 报销级别 服务实现类
 * </p>
 * @author WuZH
 * @since 2024-08-18
 */
@Service
public class KingReimburseLevelServiceImpl extends ServiceImpl<KingReimburseLevelMapper, KingReimburseLevelEntity> implements IKingReimburseLevelService {

    @Resource
    private IActModelService iActModelService;

    @Override
    public List<KingReimburseLevelEntity> queryList (KingReimburseLevelEntity kingReimburseLevelEntity) {
        return null;
    }

    @Override
    public AjaxResult draft (KingReimburseLevelEntity kingReimburseLevelEntity) {
        kingReimburseLevelEntity.setId ( SnowflakeIdWorker.getInstance ().nextStringId () );
        int count = this.count ();
        kingReimburseLevelEntity.setDocNo ( "BXJB".concat ( String.format ( "%08d", count ) ) );
        kingReimburseLevelEntity.setCreateOrgId ( CurrentUserUtil.getCompanyId () );
        kingReimburseLevelEntity.setUseOrgId ( CurrentUserUtil.getCompanyId () );
        kingReimburseLevelEntity.setCreateBy ( SecurityUtils.getLoginUser ().getUser ().getUserId () );
        kingReimburseLevelEntity.setCreateTime ( DateUtils.getNowDate () );
        kingReimburseLevelEntity.setState ( ConstUtil.STATE_IN_DRAFT );
        boolean save = this.save ( kingReimburseLevelEntity );
        if ( ! save ) {
            throw new CustomException ( "暂存报销级别失败！" );
        }
        return AjaxResult.success ( "暂存报销级别成功", kingReimburseLevelEntity.getId () );
    }

    @Override
    public AjaxResult submit (KingReimburseLevelEntity kingReimburseLevelEntity) {
        if ( StringUtils.isBlank ( kingReimburseLevelEntity.getId () ) ) {
            return AjaxResult.error ( "请先暂存该单据！" );
        }
        StartProcessDto startDto = new StartProcessDto ().
                setBusinessId ( kingReimburseLevelEntity.getId () ).
                setBusinessType ( kingReimburseLevelEntity.getBusinessType () ).setModelId ( kingReimburseLevelEntity.getModelId () ).
                setBusinessName ( "报销级别".concat ( kingReimburseLevelEntity.getId () ) );
        String instanceId = iActModelService.startProcess ( startDto );
        boolean update = this.update ( new LambdaUpdateWrapper<KingReimburseLevelEntity> ().
                set ( KingReimburseLevelEntity :: getUpdateBy, SecurityUtils.getLoginUser ().getUser () ).
                set ( KingReimburseLevelEntity :: getUpdateTime, DateUtils.getNowDate () ).
                set ( KingReimburseLevelEntity :: getInstanceId, instanceId ).
                set ( KingReimburseLevelEntity :: getState, ConstUtil.STATE_IN_PROCESS ).
                eq ( KingReimburseLevelEntity :: getId, kingReimburseLevelEntity.getId () ) );
        if ( ! update ) {
            throw new CustomException ( "提交报销级别失败！" );
        }
        return AjaxResult.success ( "提交报销级别成功" );
    }

    @Override
    public AjaxResult updateSingle (KingReimburseLevelEntity kingReimburseLevelEntity) {
        kingReimburseLevelEntity.setUpdateBy ( SecurityUtils.getLoginUser ().getUser ().getUserId () );
        kingReimburseLevelEntity.setUpdateTime ( DateUtils.getNowDate () );
        boolean b = this.updateById ( kingReimburseLevelEntity );
        if ( ! b ) {
            throw new CustomException ( "修改报销级别失败！" );
        }
        return AjaxResult.success ( "修改报销级别成功" );
    }

    @Override
    public AjaxResult deleteSingle (String[] ids) {
        boolean update = this.update ( new LambdaUpdateWrapper<KingReimburseLevelEntity> ().set ( KingReimburseLevelEntity :: getIsUsed, ConstUtil.IS_USED_NO ).
                set ( KingReimburseLevelEntity :: getUpdateTime, DateUtils.getNowDate () ).
                set ( KingReimburseLevelEntity :: getUpdateBy, SecurityUtils.getLoginUser ().getUser ().getUserId () ).
                in ( KingReimburseLevelEntity :: getId, ids ) );
        if ( ! update ) {
            throw new CustomException ( "删除报销级别失败！" );
        }
        return AjaxResult.success ( "删除报销级别成功" );
    }

    @Override
    public AjaxResult disabled (String id) {
        KingReimburseLevelEntity reimburseLevelEntity = this.getById ( id );
        boolean update = this.update ( new LambdaUpdateWrapper<KingReimburseLevelEntity> ().
                set ( KingReimburseLevelEntity :: getIsDisabled, reimburseLevelEntity.getIsDisabled () == 1 ? 2 : 1 ).
                set ( KingReimburseLevelEntity :: getDisabledTime, DateUtils.getNowDate () ).
                set ( KingReimburseLevelEntity :: getDisabledBy, SecurityUtils.getLoginUser ().getUser ().getUserId () ).
                eq ( KingReimburseLevelEntity :: getId, id ) );
        if ( ! update ) {
            throw new CustomException ( reimburseLevelEntity.getIsDisabled () == 1 ? "禁用报销级别失败！" : "解禁报销级别失败！" );
        }
        return AjaxResult.success ( reimburseLevelEntity.getIsDisabled () == 1 ? "禁用报销级别成功" : "解禁报销级别成功" );
    }
}
